package com.androider.timememory.activity;

import java.io.IOException;

import com.androider.timememory.R;
import com.androider.timememory.utils.ActivityUtil;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class AlbumPlayAudio extends Activity {

	public static String AUDIO_PATH = "AudioPath";
	public static String AUDIO_TITLE = "AudioTitle";
	private String audioPath;
	private String audioTitle;
	private Button playOrPause;
    private Button exit;
    private SeekBar seekBar;
    private MediaPlayer player;
    private TextView title;
    private Boolean isPlaying;
      
    /** Called when the activity is first created. */  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.album_play_audio);
        findView();
        setListener();
        init();
    }

    private void init() {   	
        player = new MediaPlayer();
        playOrPause.setBackgroundResource(R.drawable.audio_pause);
        try {
			if (player.isPlaying() == true) {
				player.reset();
			}
			audioPath = this.getIntent().getStringExtra(AlbumPlayAudio.AUDIO_PATH);
			player.setDataSource(audioPath);
			audioTitle = this.getIntent().getStringExtra(AlbumPlayAudio.AUDIO_TITLE);
			title.setText(audioTitle);
			player.prepare();
			player.setOnCompletionListener(new OnCompletionListener(){

				@Override
				public void onCompletion(MediaPlayer mp) {
					isPlaying = false;
            		playOrPause.setBackgroundResource(R.drawable.audio_play);
				}});
			player.start();
	        seekBar.setMax(player.getDuration());
	        handler.post(updateThread);
	        isPlaying = true;
		} catch (Exception e) {
			ActivityUtil.showLongTip(this, R.string.tip_audio_not_found);
			e.printStackTrace();
			this.finish();
		}
    }
    private void findView() {
        playOrPause = (Button)findViewById(R.id.playOrPause);
        seekBar = (SeekBar)findViewById(R.id.seekBar);
        title = (TextView) findViewById(R.id.audio_note);
    }
      
    Handler handler = new Handler();
    Runnable updateThread = new Runnable(){
        public void run() {
        	seekBar.setProgress(player.getCurrentPosition());
            handler.postDelayed(updateThread, 100);
            
            if(player.getCurrentPosition() == player.getDuration()){
            	playOrPause.setBackgroundResource(R.drawable.audio_play);
            }
        }
    };
      
    private void setListener() {
        playOrPause.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0){
            	if(isPlaying = !isPlaying){
                    player.start();
                    handler.post(updateThread);
                    playOrPause.setBackgroundResource(R.drawable.audio_pause);
            	}else{
            		player.pause();
            		handler.removeCallbacks(updateThread);
            		playOrPause.setBackgroundResource(R.drawable.audio_play);
            	}
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                if(fromUser==true){
                    player.seekTo(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }  
            @Override  
            public void onStopTrackingTouch(SeekBar seekBar) {
            }  
        });
    }

	@Override
	protected void onDestroy() {
		player.stop();
		player.release();
        handler.removeCallbacks(updateThread); 
		super.onDestroy();
	}                     
}